﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Concurrency;
using System.Threading;
using System.Windows.Data;
using System.ComponentModel;
using Caliburn.PresentationFramework;
using IronCommander.ViewModels;
using System.IO;
using IronCommander.Utils;

namespace IronCommander.Domain
{
    public class FileLoader
    {
        readonly IFileSystemInfoProvider fileSystemInfoProvider;

        public FileLoader(IFileSystemInfoProvider fileSystemInfoProvider)
        {
            this.fileSystemInfoProvider = fileSystemInfoProvider;
        }

        public BindableCollection<FileViewModel> Load(string path)
        {
            var files = new BindableCollection<FileViewModel>();

            files.AddRange(GetFiles(path));

            return files;
        }

        private IEnumerable<FileViewModel> GetFiles(string path)
        {
            var files = fileSystemInfoProvider.GetFiles(path);

            If.Value(Path.GetDirectoryName(path)).IsNotNull(parentPath =>
                files = files.StartWith(new FileViewModel(new DirectoryInfo(Path.GetDirectoryName(path)))
                {
                    IsParentPath = true,
                    DisplayName = ".."
                }));

            return files;
        }
    }
}
